﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.ObjectModel;
using DiscountCoupon.Phone.Common;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Linq;
using System.ComponentModel;
using System.Collections.Specialized;

namespace DiscountCoupon
{
    /// <summary>
    /// 品牌控制器
    /// </summary>
    public partial class MainPage
    {
        ObservableCollection<CardSeller> OCSellerList;

        private const string SETTING_KEY = "SETTING_KEy";
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Second_ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.IsSellerListLoaded)
                return;
            dwHelper.StartRequest("http://loveios.com/aspx/app/DC/seller.aspx", 
                UriKind.Absolute,
                "CardSellert",
                helper_onRequestCardSellertCompleted);

            this.loadingProgressBar.IsIndeterminate = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void helper_onRequestCardSellertCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer serrilizer = new DataContractJsonSerializer(typeof(List<CardSeller>));
            object obj = serrilizer.ReadObject(e.Result);
            IList<CardSeller> sellerList = obj as List<CardSeller>;
           
            if (sellerList != null)
            {
                int[] configDisplayer = GetSettingSeller();
                foreach (CardSeller item in sellerList)
                {
                    if (!configDisplayer.Contains(item.SellerId))
                    {
                        item.IsDisplay = true;
                    }
                }
                OCSellerList = new ObservableCollection<CardSeller>(sellerList);
                this.SecondListBoxSeller.ItemsSource = OCSellerList;               
            }
            App.ViewModel.IsSellerListLoaded = true;
        }

        private void On_Seller_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            //MessageBox.Show(((int)cb.Tag).ToString());
            int[] ids = GetSettingSeller();
            if (cb.IsChecked.HasValue && !cb.IsChecked.Value && !ids.Contains((int)cb.Tag))
            {
                List<int> l= ids.ToList();
                l.Add((int)cb.Tag);
                SaveSettingSeller(l.ToArray());
            }
            if (cb.IsChecked.HasValue && cb.IsChecked.Value && ids.Contains((int)cb.Tag))
            {
                List<int> l = ids.ToList();
                l.Remove((int)cb.Tag);
                SaveSettingSeller(l.ToArray());
            }
            ///刷新列表
            RetriveCardData();
        }
        
        /// <summary>
        /// 取不需要显示的供应品牌
        /// </summary>
        /// <returns></returns>
        private int[] GetSettingSeller()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(SETTING_KEY))
            {
                return (int[])IsolatedStorageSettings.ApplicationSettings[SETTING_KEY];
            }
            else
                return new int[]{};
        }

        /// <summary>
        /// 保存不需要显示的品牌
        /// </summary>
        /// <param name="seller"></param>
        private void SaveSettingSeller(int[] seller)
        {
            IsolatedStorageSettings.ApplicationSettings[SETTING_KEY] = seller;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }
}
